package org.sdnstproject.interlayer.OF13Protocal;

import java.util.Arrays;
import java.util.Random;

import org.projectfloodlight.openflow.types.U16;
import org.projectfloodlight.openflow.types.U32;
import org.projectfloodlight.openflow.types.U8;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuFlowStatsRequest {
	private static final Logger log = LoggerFactory.getLogger(FuFlowStatsRequest.class);
    public byte version = (byte)4;
    public byte type = (byte)18;
    public short length = (short)56;//MINIMUM_LENGTH = 56;
    public long xid = 0x0L;
    public short statsType = (short)1;// fixed value property statsType = 1
    public int flags =(short) 0x1;
    public short tableId = 0x00FF;//TableId  default ALL_VAL = 0x00FF;
    public long outPort = 0xFFffFFff;//OFPort
    public long outGroup = 0xffffffff;//OFGroup
    public long cookie = 0x0L;
    public long cookieMask = 0x0L;
    public byte[] match = new byte[] {};//TODO
    
	
	public FuFlowStatsRequest() {
		
	}
	
	public FuFlowStatsRequest setXid(long xid) {
		this.xid = xid;
		return this;
	}
	
	public FuFlowStatsRequest setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "statsType":
			this.statsType = Short.parseShort(value);
			break;
		case "flags":
			this.flags = Integer.parseInt(value);
			break;
		case "tableId":
			this.tableId = Short.parseShort(value);
			break;
		case "outPort":
			this.outPort = Long.parseLong(value);
			break;
		case "outGroup":
			this.outGroup = Long.parseLong(value);
			break;
		case "cookie":
			this.cookie = Long.parseLong(value);
			break;
		case "cookieMask":
			this.cookieMask = Long.parseLong(value);
			break;
		case "match":
			this.match = new byte[(new Random().nextInt(5)+1)*8+4];
			new Random().nextBytes(match);
			break;
		}
		return this;
	}
	
	public ByteBuf writeTo() {
		ByteBuf bb = Unpooled.directBuffer();
		int startIndex = bb.writerIndex();
        // fixed value property version = 4
        bb.writeByte(version);
        // fixed value property type = 18
        bb.writeByte(type);
        // length is length of variable message, will be updated at the end
        int lengthIndex = bb.writerIndex();
        bb.writeShort(length);

        bb.writeInt(U32.t(xid));
        // fixed value property statsType = 1
        bb.writeShort(statsType);
        bb.writeShort(U16.t(flags));
        // pad: 4 bytes
        bb.writeZero(4);
        bb.writeByte(U8.t(tableId));
        // pad: 3 bytes
        bb.writeZero(3);
        bb.writeInt(U32.t(outPort));
        bb.writeInt(U32.t(outGroup));
        // pad: 4 bytes
        bb.writeZero(4);
        bb.writeLong(cookie);
        bb.writeLong(cookieMask);
        bb.writeBytes(match);

        // update length field
        length = (short)(bb.writerIndex() - startIndex);
        bb.setShort(lengthIndex, length);
        return bb;
    }
	
	@Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFFlowStatsRequestVer13(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(",");
        b.append("flags=").append(flags);
        b.append(", ");
        b.append("tableId=").append(tableId);
        b.append(", ");
        b.append("outPort=").append(outPort);
        b.append(", ");
        b.append("outGroup=").append(outGroup);
        b.append(", ");
        b.append("cookie=").append(cookie);
        b.append(", ");
        b.append("cookieMask=").append(cookieMask);
        b.append(", ");
        b.append("match=").append(Arrays.toString(match));
        b.append(")");
        return b.toString();
    }
}
